package com.example.wo.jsbapplication;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.List;

public class NoteDao {
    private  NoteData noteData;


    public NoteDao(Context context) {
        noteData = new NoteData(context, "note.db", null, 2);
    }
    public void insert(Note note){
        SQLiteDatabase db =noteData.getWritableDatabase();
        ContentValues values =new ContentValues();
        values.put("title",note.getTitle());
        values.put("content",note.getContent());
        long id = db.insert("tb_note",null,values);
        note.setId(id);
        db.close();
    }
    public  int delete(Long id){
        SQLiteDatabase db = noteData.getWritableDatabase();
        int count = db.delete("tb_note","id=?",new String[]{id+""});
        db.close();
        return count;
    }
    public List<Note> getAll(){
        SQLiteDatabase db = noteData.getReadableDatabase();
        Cursor cursor = db.query("tb_note",null,null,
                null,null,null,null);
        List<Note> notes = new ArrayList<>();
        while (cursor.moveToNext()){
            long id =cursor.getLong(cursor.getColumnIndex("id"));
            String title = cursor.getString(1);
            String content =cursor.getString(2);
            notes.add(new Note(id,title,content));
        }
        cursor.close();
        db.close();
        return notes;
    }


}
